<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.6
* @copyright Copyright (C) 2006-2009 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

if( defined('_JEXEC') ){
	//joomla 1.5
	$task = JRequest::getVar('task', '', 'get');
}else{
	//joomla 1.0.x
	$task = mosGetParam( $_GET, 'task', '' );
}

global $mainframe, $my;

switch ($task) {
	case 'item_type_select':
		item_type_select();
		break;
	case 'item':
		show_item_edit_page();
		break;
	case 'item_save':
		item_save();
		break;
	case 'itemtype':
		display_itemtype();
		break;	
	case 'download':
		do_download();
		break;
}

function display_itemtype(){

	if( defined('_JEXEC') ){
		//joomla 1.5
		$plugin_dir = 'plugins';
		$item_type = JRequest::getVar('item_type', '', 'get');
	}else{
		//joomla 1.0.x
		$plugin_dir = 'mambots';
		$item_type = mosGetParam( $_GET, 'item_type', '' );
	}	
	$item_type_array = array('googlevideo', 'image_gallery', 'module_position', 'php', 'youtube');
	if(in_array($item_type, $item_type_array) && file_exists(dirname(__FILE__).'/../../'.$plugin_dir.'/pages_and_items/itemtypes/'.$item_type.'/'.$item_type.'_frontend.php')){		
		include(dirname(__FILE__).'/../../'.$plugin_dir.'/pages_and_items/itemtypes/'.$item_type.'/'.$item_type.'_frontend.php');
	}else{
		exit;
	}
	
}

function item_save(){
	
	//include class	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/class.php');
	$class_pi = new class_pi();	
	
	//include language	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/language/'.$class_pi->pi_config['language'].'.php');
	
	$class_pi->item_save();		
}

function item_type_select(){
	
	$frontend = true;
	
	//include class	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/class.php');
	$class_pi = new class_pi();

	if(!$class_pi->pi_config['item_type_select_frontend']){
		exit('itemtype selection from the frontend is disabled');
	}

	//include language	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/language/'.$class_pi->pi_config['language'].'.php');
	
	//include class	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/class.php');
	$class_pi = new class_pi();
	
	$allowed_user_types = array('Author','Editor','Publisher','Manager','Administrator','Super Administrator');		
	if(!in_array($class_pi->user_type, $allowed_user_types)){
		exit('You have no permission to edit content');
	}
	
	$page_id = intval($class_pi->get_var('Itemid', '', 'get'));		
	
	$js = 'function new_item(page_id){'."\n";	
	$js .= 'itemtype = document.getElementById(\'select_itemtype\').value;'."\n";		
	$js .= 'document.location.href=\'index.php?option=com_pi_pages_and_items&type=content_blog_category&task=item&sub_task=new&pageId=\'+page_id+\'&item_type=\'+itemtype;'."\n";		
	$js .= '}'."\n";			
	
	if(defined('_JEXEC')){
		//joomla 1.5
		$doc =&JFactory::getDocument();
		$doc->addScriptdeclaration($js);
	}else{
		//joomla 1.0.x
		echo '<script language="javascript" type="text/javascript">'."\n";	
		echo $js;
		echo '</script>'."\n";
	}
	
	echo '<div class="componentheading" style="margin-bottom: 50px;">'._pi_lang_select_itemtype_for_new_item.'</div>';
	
	$class_pi->itemtype_select($page_id);
	
	echo '<br /><br /><br /><br /><br /><br />';

}

function show_item_edit_page(){	

	global $mainframe;	
	
	//get vars
	if( defined('_JEXEC') ){
		//joomla 1.5			
		$sub_task = JRequest::getVar('sub_task', '', 'get');
		$item_id = intval(JRequest::getVar('item_id', '', 'get'));	
		$item_type = JRequest::getVar('item_type', '', 'get');		
	}else{
		//joomla 1.0.x		
		$sub_task = mosGetParam( $_GET, 'sub_task', '' );	
		$item_id = intval(mosGetParam( $_GET, 'item_id', '' ));	
		$item_type = mosGetParam( $_GET, 'item_type', '' );			
	}
	
	//no messing about
	if($sub_task!='new' && $sub_task!='edit'){
		//exit('stop messing about');
		$sub_task = 'edit';
	}
	
	$frontend = true;	
	
	//include class	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/class.php');
	$class_pi = new class_pi();	
	
	//include language	
	require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/language/'.$class_pi->pi_config['language'].'.php');
	
	if(!$class_pi->pi_config['use_pi_frontend_editting']){
		exit(_pi_lang_frontend_editting_disabled);
	}	
	
	if($item_type!='text' && $item_type!='' && !$class_pi->pi_config['item_type_not_text_frontend'] && $sub_task=='new'){		
		exit(_pi_lang_only_text_frontend_new);
	}	
	
	if($item_type!='text' && $item_type!='' && !$class_pi->pi_config['item_type_not_text_frontend_edit'] && $sub_task=='edit'){		
		exit(_pi_lang_only_text_frontend_edit);
	}	
	
	$allowed_user_types = array('Author','Editor','Publisher','Manager','Administrator','Super Administrator');		
	if(!in_array($class_pi->user_type, $allowed_user_types)){	
		exit(_pi_lang_no_edit_or_logged_in);
	}
	
	//unlock item which com_content has just locked before the redirect
	$class_pi->unlock_item($item_id);	
	
	//include item-edit-page
	include(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/admin/item.php');

}


function do_download(){	
	if( defined('_JEXEC') ){
		//joomla 1.5
		$id = intval(JRequest::getVar('id', '', 'get'));
	}else{
		//joomla 1.0.x
		$id = intval(mosGetParam($_GET, 'id', 0 ));
	}
	$error = 'file can not be downloaded';	
	if($id){		
		
		//include class	
		require_once(dirname(__FILE__).'/../../administrator/components/com_pi_pages_and_items/class.php');
		$class_pi = new class_pi();	
		
		//get config
		$pi_config = $class_pi->pi_config;	
			
		$database = get_database();
				
		//get filename, mimetype and hits from database
		$database->setQuery("SELECT file_name, type, hits FROM #__pi_downloads WHERE id='$id' LIMIT 1");
		$rows = $database->loadObjectList();
		$row = $rows[0];
		$file_name = $row->file_name;		
						
		//get extension
		$extension = explode(".", $file_name);
		$last = count($extension) - 1;
		$extension = strtolower($extension[$last]);	
		
		//get allowed extensions		
		$allowed_extensions = explode(" ", $pi_config['download_filetypes']);		
		
		if($pi_config['download_debugger']==true){
			if(!file_exists(dirname(__FILE__).'/../../media/'.$file_name)){
				echo 'debugger: no such file';
			}else if(!in_array($extension, $allowed_extensions)){
				echo 'debugger: extension is not allowed';			
			}else if($pi_config['download_allow']==false){
				echo 'debugger: no downloads allowed at all';
			}else{
				echo 'debugger: file should download. disable the download-debugger in the Pages-and-Items configuration.';
			}
			exit;
		}			
		
		if(file_exists(dirname(__FILE__).'/../../media/'.$file_name) && in_array($extension, $allowed_extensions) && $pi_config['download_allow']==true){
					
			//do download
			ob_end_clean();	
			header("Cache-Control: public, must-revalidate");
			header('Cache-Control: pre-check=0, post-check=0, max-age=0');
			header("Pragma: no-cache");
			header("Expires: 0"); 
			header("Content-Description: File Transfer");
			header("Expires: Sat, 30 Dec 1990 07:07:07 GMT");		
			header("Content-Type: ". $row->type);
			header("Content-length: ". filesize(dirname(__FILE__).'/../../media/'.$file_name));
			header('Content-Disposition: attachment; filename="' . $file_name . '"');
			header("Content-Transfer-Encoding: binary\n");	
			@readfile(dirname(__FILE__).'/../../media/'.$file_name);	
			
			//update hits		
			$new_hits = $row->hits+1;
			$database->setQuery( "UPDATE #__pi_downloads SET hits='$new_hits' WHERE id='$id'"	);
			$database->query();	
			
			exit;		
		}else{
			echo $error;
			exit;
		}		
	}else{
		echo $error;
		exit;
	}	
}

function get_database(){
	//get database
	if( defined('_JEXEC') ){
		//joomla 1.5
		$database = JFactory::getDBO();
	}else{
		//joomla 1.0.x
		global $database;
		$database = $database;
	}
	return $database;	
}


?>